package com.yj.web.service.impl;

import com.yj.web.dao.mapper.AgreementMapper;
import com.yj.web.dao.po.admin.Agreement;
import com.yj.web.help.PoUtil;
import com.yj.web.service.AgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yanlinyang on 2017/7/4.
 */
@Service("agreementService")
public class AgreementServiceImpl implements AgreementService {

    @Autowired
    private AgreementMapper agreementMapper;

    @Override
    public List<Agreement> listAgreement(int type, int page, int size) {
        int offset = (page - 1) * size;
        List<Agreement> agreements = agreementMapper.listAgreement(type, offset, size);
        return agreements;
    }

    @Override
    public int countAgreement(int type) {
        int num = agreementMapper.countAgreement(type);
        return num;
    }

    @Override
    public int deleteAgreementById(int id) {
        Agreement agreement = agreementMapper.findAgreementById(id);
        int activeId = agreementMapper.getActiveId(agreement.getType());
        if (id == activeId) {
            return 0;
        }
        int num = agreementMapper.deleteAgreementById(id);
        return num;
    }

    @Override
    public long insertAgreement(Agreement agreement) {
        int count = agreementMapper.countAgreement(agreement.getType());
        if (0 == count) {
            if (agreement.getStatus() == 0) {
                return 0;
            }
        } else {
            if (1 == agreement.getStatus()) {
                int activeId = agreementMapper.getActiveId(agreement.getType());
                agreementMapper.updateAgreementStatus(activeId, 0);
            }
        }
        return agreementMapper.insertAgreement(agreement);
    }

    @Override
    public boolean updateAgreement(Agreement agreement) {
        Agreement agreementOriginal = agreementMapper.findAgreementById(agreement.getId());
        if(agreementOriginal == null) {
            return false;
        }
        try {
            PoUtil.supplementPo(agreement, agreementOriginal);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        if (agreementOriginal.getStatus() == 1) {
            int activeId = agreementMapper.getActiveId(agreementOriginal.getType());
            if (activeId != agreement.getId()) {
                agreementMapper.updateAgreementStatus(activeId, 0);
            }
        }
        return agreementMapper.updateAgreement(agreementOriginal);
    }

    @Override
    public Agreement getAgreementById(int id) {
        Agreement agreement = agreementMapper.findAgreementById(id);
        return agreement;
    }

    @Override
    public boolean updateAgreementStatus(int id) {
        Agreement agreement = agreementMapper.findAgreementById(id);
        int activeId = agreementMapper.getActiveId(agreement.getType());
        if (id == activeId) {
            return false;
        } else {
            agreementMapper.updateAgreementStatus(activeId, 0);
            return agreementMapper.updateAgreementStatus(id, 1);
        }
    }

}
